﻿/*********************************************************************
 版权所有: 版权所有(C) 2010 ,熙软研发部
 模块编号:  ContactGroupDO
 作    者:范徐超
 编写日期:2010-3-6
 审    核:
 审核日期:
 描    述:ContactGroupDO 短信草稿箱  数据访问类
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using Cisoft.IntegrationSystem.DataHelper;

namespace Cisoft.Cisoft3G.DAL
{
    /// <summary>
    /// 数据访问类DraftsDO。
    /// </summary>
    public class DraftsDO
    {
        public DraftsDO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SQLServerHelper.GetMaxID("DT_DraftID", "CG_Drafts");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DT_DraftID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CG_Drafts");
            strSql.Append(" where DT_DraftID=@DT_DraftID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DT_DraftID", SqlDbType.Int,4)};
            parameters[0].Value = DT_DraftID;

            return SQLServerHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Cisoft.Cisoft3G.Model.DraftsMO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CG_Drafts(");
            strSql.Append("DT_DraftID,DT_DraftName,DT_Receiver,DT_SmsContent,DT_DraftRemark,DT_SendTime)");
            strSql.Append(" values (");
            strSql.Append("@DT_DraftID,@DT_DraftName,@DT_Receiver,@DT_SmsContent,@DT_DraftRemark,@DT_SendTime)");
            SqlParameter[] parameters = {
					new SqlParameter("@DT_DraftID", SqlDbType.Int,4),
					new SqlParameter("@DT_DraftName", SqlDbType.NVarChar,50),
					new SqlParameter("@DT_Receiver", SqlDbType.NVarChar,3000),
					new SqlParameter("@DT_SmsContent", SqlDbType.NVarChar,1000),
					new SqlParameter("@DT_DraftRemark", SqlDbType.NVarChar,1000),
                                        new SqlParameter("@DT_SendTime", SqlDbType.DateTime)};
            parameters[0].Value = model.DT_DraftID;
            parameters[1].Value = model.DT_DraftName;
            parameters[2].Value = model.DT_Receiver;
            parameters[3].Value = model.DT_SmsContent;
            parameters[4].Value = model.DT_DraftRemark;
            parameters[5].Value = model.DT_SendTime;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Cisoft.Cisoft3G.Model.DraftsMO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CG_Drafts set ");
            strSql.Append("DT_DraftName=@DT_DraftName,");
            strSql.Append("DT_Receiver=@DT_Receiver,");
            strSql.Append("DT_SmsContent=@DT_SmsContent,");
            strSql.Append("DT_DraftRemark=@DT_DraftRemark,");
            strSql.Append("DT_SendTime=@DT_SendTime");
            strSql.Append(" where DT_DraftID=@DT_DraftID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DT_DraftID", SqlDbType.Int,4),
					new SqlParameter("@DT_DraftName", SqlDbType.NVarChar,50),
					new SqlParameter("@DT_Receiver", SqlDbType.NVarChar,3000),
					new SqlParameter("@DT_SmsContent", SqlDbType.NVarChar,1000),
					new SqlParameter("@DT_DraftRemark", SqlDbType.NVarChar,1000),
                    new SqlParameter("@DT_SendTime", SqlDbType.DateTime)};
            parameters[0].Value = model.DT_DraftID;
            parameters[1].Value = model.DT_DraftName;
            parameters[2].Value = model.DT_Receiver;
            parameters[3].Value = model.DT_SmsContent;
            parameters[4].Value = model.DT_DraftRemark;
            parameters[5].Value = model.DT_SendTime;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int DT_DraftID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CG_Drafts ");
            strSql.Append(" where DT_DraftID=@DT_DraftID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DT_DraftID", SqlDbType.Int,4)};
            parameters[0].Value = DT_DraftID;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cisoft.Cisoft3G.Model.DraftsMO GetModel(int DT_DraftID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DT_DraftID,DT_DraftName,DT_Receiver,DT_SmsContent,DT_DraftRemark,DT_SendTime from CG_Drafts ");
            strSql.Append(" where DT_DraftID=@DT_DraftID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DT_DraftID", SqlDbType.Int,4)};
            parameters[0].Value = DT_DraftID;

            Cisoft.Cisoft3G.Model.DraftsMO model = new Cisoft.Cisoft3G.Model.DraftsMO();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["DT_DraftID"].ToString() != "")
                {
                    model.DT_DraftID = int.Parse(ds.Tables[0].Rows[0]["DT_DraftID"].ToString());
                }
                model.DT_DraftName = ds.Tables[0].Rows[0]["DT_DraftName"].ToString();
                model.DT_Receiver = ds.Tables[0].Rows[0]["DT_Receiver"].ToString();
                model.DT_SmsContent = ds.Tables[0].Rows[0]["DT_SmsContent"].ToString();
                model.DT_DraftRemark = ds.Tables[0].Rows[0]["DT_DraftRemark"].ToString();

                if (ds.Tables[0].Rows[0]["DT_SendTime"].ToString() != "")
                {
                    model.DT_SendTime = DateTime.Parse(ds.Tables[0].Rows[0]["DT_SendTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DT_DraftID,DT_DraftName,DT_Receiver,DT_SmsContent,DT_DraftRemark,DT_SendTime ");
            strSql.Append(" FROM CG_Drafts ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLServerHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DT_DraftID,DT_DraftName,DT_Receiver,DT_SmsContent,DT_DraftRemark,DT_SendTime ");
            strSql.Append(" FROM CG_Drafts ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLServerHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "CG_Drafts";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SQLServerHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法

        public DataSet GetListWithPage(int pageCurrent, int pageSize, string strWhere)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameters = {
                    new SqlParameter("@tbname", SqlDbType.VarChar,500),
                    new SqlParameter("@FieldKey", SqlDbType.VarChar,500),
                    new SqlParameter("@PageCurrent", SqlDbType.Int,4),
                    new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@FieldShow", SqlDbType.VarChar,50),
                    new SqlParameter("@FieldOrder", SqlDbType.VarChar,500),
                    new SqlParameter("@Where", SqlDbType.VarChar,500),
                    new SqlParameter("@ItemCount", SqlDbType.Int,4),
                    new SqlParameter("@PageCount", SqlDbType.Int,4)
                    };
            parameters[0].Value = "CG_Drafts";
            parameters[1].Value = "DT_DraftID";
            parameters[2].Value = pageCurrent;
            parameters[3].Value = pageSize;
            parameters[4].Value = "";
            parameters[5].Value = "DT_SendTime desc";
            parameters[6].Value = strWhere;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            ds = SQLServerHelper.ExecuteStoredProcedure("sp_PageView", parameters);
            return ds;
        }


        public int GetListCount(int pageCurrent, int pageSize, string strWhere)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameters = {
                    new SqlParameter("@tbname", SqlDbType.VarChar,500),
                    new SqlParameter("@FieldKey", SqlDbType.VarChar,500),
                    new SqlParameter("@PageCurrent", SqlDbType.Int,4),
                    new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@FieldShow", SqlDbType.VarChar,50),
                    new SqlParameter("@FieldOrder", SqlDbType.VarChar,500),
                    new SqlParameter("@Where", SqlDbType.VarChar,500),
                    new SqlParameter("@ItemCount", SqlDbType.Int,4),
                    new SqlParameter("@PageCount", SqlDbType.Int,4)
                    };
            parameters[0].Value = "CG_Drafts";
            parameters[1].Value = "DT_DraftID";
            parameters[2].Value = pageCurrent;
            parameters[3].Value = pageSize;
            parameters[4].Value = "";
            parameters[5].Value = "DT_SendTime desc";
            parameters[6].Value = strWhere;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            object obj = SQLServerHelper.ExecuteStoredProcedure("sp_PageView", parameters, "@ItemCount");
            return Convert.ToInt32(obj);
        }
    }
}

